package com.ctf.soft.ms.controls.manage;

import com.ctf.soft.ms.dao.dto.manage.Pager;
import com.ctf.soft.ms.dao.model.manage.Datasource;
import com.ctf.soft.ms.service.manage.IDatasourceService;
import com.ctf.soft.ms.utils.AnnotationUtil;
import com.ctf.soft.ms.utils.ToolsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.persistence.Column;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 名称： DatasourceController<br>
 * 描述： 数据源配置控制器<br>
 * 最近修改时间：2015/7/31 13:32<br>
 * 修改说明：
 *
 * @author jiajl
 */
@Controller
@RequestMapping("/datasource")
public class DatasourceController {

    @Autowired
    private IDatasourceService datasourceService;

    @RequestMapping("/view")
    public String doView() {
        return "manage/datasource/view";
    }

    @RequestMapping("/editor")
    public String doEditor() {
        return "manage/datasource/editor";
    }

    @RequestMapping("/SaveOrUpdate")
    @ResponseBody
    public String doSaveOrUpdate(@RequestBody Datasource datasource,
                                 HttpServletRequest request) {
        String result = "faild";
        try {
            int c = -1;
            if (ToolsUtil.isEmpty(datasource.getId())) {
                datasource.setId(ToolsUtil.getRandomForUUID());
                c = datasourceService.insertSelective(datasource);
            } else {
                c = datasourceService.updateByPrimaryKeySelective(datasource);
            }
            if (c > 0) {
                result = "success";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/Delete")
    @ResponseBody
    public String doDelete(@RequestParam("id") String id,
                           HttpServletRequest request) {
        String result = "faild";
        try {
            int c = -1;
            Datasource params = new Datasource();
            params.setId(id);
            params.setStatus("D");
            c = datasourceService.updateByPrimaryKeySelective(params);
            if (c > 0) {
                result = "success";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/Data")
    @ResponseBody
    public Datasource loadData(@RequestParam("id") String id,
                               HttpServletRequest request) {
        Datasource datasource = null;
        try {
            datasource = datasourceService.selectByPrimaryKey(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return datasource;
    }

    @RequestMapping("/List")
    @ResponseBody
    public List<Datasource> loadList(HttpServletRequest request) {
        List<Datasource> list = null;
        try {
            Datasource params = new Datasource();
            params.setStatus("Y");
            list = datasourceService.selectByList(params);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    @RequestMapping("/PageList")
    @ResponseBody
    public Pager<Datasource> loadPageList(@RequestBody ModelMap params,
                                          HttpServletRequest request) {
        Pager<Datasource> pager = null;
        try {
            params.put("status", "D");
            String sidx = (String) params.get("sidx");
            String sord = (String) params.get("sord");
            params.put("sord", sord);
            params.put("sidx", AnnotationUtil.getColumnValue(Datasource.class, Column.class, sidx, "name"));
            int totalRecord = datasourceService.getPageCount(params);
            int page = Integer.valueOf(params.get("page").toString());
            int rows = Integer.valueOf(params.get("rows").toString());
            int totalPage = totalRecord % rows == 0 ? totalRecord / rows
                    : totalRecord / rows + 1;
            List<Datasource> list = datasourceService.loadPageList(params);
            if (list != null) {
                pager = new Pager<Datasource>();
                pager.setCurrPage(page);
                pager.setTotalPage(totalPage);
                pager.setTotalRecord(totalRecord);
                pager.setList(list);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pager;
    }
}
